import { ListBase } from 'src/components/list_base_abs';
import { Component, Emit, Prop, Provide, Watch } from 'vue-property-decorator'

import { WebManageConst } from "src/consts/web_manage_const";
import { updateSelection } from 'src/utils/list_utils';
import OperatorHandler from 'src/views/web_manage/operator_handler';
import ListDemoHandler from 'src/views/web_manage/list_handler';
import * as Api from 'src/api/net';

@Component({})
export default class WebManage extends ListBase {
    operatorData: OperatorHandler;
    listHandler: ListDemoHandler;
    isReset: Boolean = false;
    isQiYong: Boolean = false;
    isTingYong: Boolean = false;
    isAdmin: Boolean;
    mainData: any;
    advData: any;
    queryData: any;
    feature_code: String;
    reqUrl: String;
    activeRouteItemRoute: string;

    initData(that: WebManage) {
        that.myState = this.$store.state.webManage;
        that.selection = [];

        that.operatorData = new OperatorHandler();
        that.listHandler = new ListDemoHandler();
        that.operatorData.init(this);
        that.isAdmin = false
        var authority = getUserInfo().authority_list
        var req = ""
        var flag = false

        for (let i = 0; i < authority.length; i++) {
            if (authority[i].name == "公共网站管理") {
                for (let j = 0; j < authority[i].child_list.length; j++) {
                    if (authority[i].child_list[j].name == "网站访问") {
                        for (let k = 0; k < authority[i].child_list[j].child_list.length; k++) {
                            if (authority[i].child_list[j].child_list[k].id == "ROLE_webapply_save") {
                                that.isAdmin = true
                            }
                        }
                    }
                    if (authority[i].child_list[j].name == "网站管理") {
                        for (let k = 0; k < authority[i].child_list[j].child_list.length; k++) {
                            if (authority[i].child_list[j].child_list[k].id == "ROLE_website_manage_admin_list") {
                                flag = true
                            }
                        }
                    }
                }
            }
        }

        if (flag) {
            that.reqUrl = "/website/manage/admin/list.do"
        } else {
            that.reqUrl = "/website/manage/normal/list.do"
        }

        that.mainData = {}
        that.advData = {}
        that.queryData = {};
        that.feature_code = Config.featureCode2 + 'web.model.WebsiteView'
    }

    getListData(): void {
        let sendData: any = {}
        sendData["curr_page"] = this.myState.curr_page
        sendData["page_size"] = this.myState.page_size
        WLDispatch(WebManageConst.DispatchWebMList, sendData);
    }

    mounted(): void {
        this.getListData();
    }

    doQuery(isAdv: Boolean): void {
        var obj: any = {}
        for (let key in this.mainData) {
            if (this.mainData[key]) {
                obj[key] = this.mainData[key]
                if (key == "name") {
                    obj[key] = this.mainData[key].name
                }
            }
        }

        if (isAdv) {
            for (let key in this.advData) {
                if (this.advData[key]) {
                    obj[key] = this.advData[key]
                    if (key == "date") {
                        obj.start_date = this.advData[key][0]
                        obj.end_date = this.advData[key][1]
                    }

                    if (key == "create_name") {
                        obj[key] = this.advData[key].name
                    }
                }
            }
        }


        this.queryData = obj

        obj["curr_page"] = this.myState.curr_page
        obj["page_size"] = this.myState.page_size
        WLDispatch(WebManageConst.DispatchWebMList, obj);
    }

    get _activeRouteItemRoute(): string {
        return this.$route.path
    }

    @Watch('_activeRouteItemRoute', { immediate: true })
    onRouteChanged(val: any) {
        if (val == "/web_manage") {
            this.getListData()
        }
    }

    expandChange(data: any): void {

    }

    setDate(date: String | Number): String {
        return timestampToTime(date)
    }

    setVal(data: string): String {
        return getValueByKey("login_type", data)
    }

    setName(data: string | Number): String {
        if (data) {
            var apply_data: any = {}
            apply_data.id = data
            Api.AjaxPost(Config.ajaxUrl + "/account/show.do", apply_data, (res: any) => {
                if (isNoEmptyObject(res.data)) {
                    return res.data.name;
                }
            })
        }

        return ""
    }

    sizeChange(size: number): void {
        let senData: any = this.queryData
        senData["curr_page"] = this.myState.curr_page
        senData["page_size"] = size
        WLDispatch(WebManageConst.DispatchWebMList, senData);
    }

    pageChange(page: number): void {
        let senData: any = this.queryData
        senData["curr_page"] = page
        senData["page_size"] = this.myState.page_size
        WLDispatch(WebManageConst.DispatchWebMList, senData);
    }

    handleSelectionChange(list: any[]): void {
        updateSelection(this.selection, list);
        this.handleBtnShow(this.selection)
        console.log("this selection", this.selection, this.selection.length);
    }

    handleBtnShow(data: any): void {
        if (data.length > 0) {
            if (data[0].web_status == "qiyong") {
                this.isTingYong = true;
                this.isQiYong = false;
            } else if (data[0].web_status == "tingyong") {
                this.isQiYong = true;
                this.isTingYong = false;
            }
            this.isReset = true
            if (data.length > 1) {
                for (let i = 0; i < data.length; i++) {
                    if (i > 0) {
                        if (data[i].web_status == data[i - 1].web_status) {
                            if (data[i].web_status == "qiyong") {
                                this.isTingYong = true;
                                this.isQiYong = false;
                            } else if (data[i].web_status == "tingyong") {
                                this.isQiYong = true;
                                this.isTingYong = false;
                            }
                        } else {
                            this.isQiYong = false;
                            this.isTingYong = false;
                            return
                        }
                    }
                }
            }

        } else {
            this.isReset = false
            this.isQiYong = false;
            this.isTingYong = false;
        }
    }

    rowClassName(data: any): string {
        if (data.row.selected) {
            return 'selected_row';
        }
        return '';
    }

    doNew(): void {
        if (this.operatorData && this.operatorData.doNew) {
            this.operatorData.doNew();
        } else {
            this.$message.warning("todo doNew 方法");
        }
    }

    doDownLoad(): void {
        if (this.operatorData && this.operatorData.doDownLoad) {
            this.operatorData.doDownLoad();
        } else {
            this.$message.warning("todo doDownLoad 方法");
        }
    }

    doUpLoad(): void {
        if (this.operatorData && this.operatorData.doUpLoad) {
            this.operatorData.doUpLoad();
        } else {
            this.$message.warning("todo doUpLoad 方法");
        }
    }


    doReset(): void {
        if (this.operatorData && this.operatorData.doReset) {
            var idArr = []
            for (let i = 0; i < this.selection.length; i++) {
                idArr.push(this.selection[i].id)
            }
            this.operatorData.doReset(idArr);
        } else {
            this.$message.warning("todo doReset 方法");
        }
    }

    doQiYong(): void {
        this.$confirm('是否确定启用网站?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            if (this.operatorData && this.operatorData.doQiYong) {
                for (let i = 0; i < this.selection.length; i++) {
                    let sendData: any = {};
                    sendData["id"] = this.selection[i].id
                    sendData["web_status"] = "qiyong"
                    sendData["type"] = "qy"
                    sendData["flag"] = false
                    if (i == this.selection.length - 1) { sendData["flag"] = true }
                    this.operatorData.doQiYong(sendData);
                }
            }
        }).catch(() => { })
    }

    doTingYong(): void {
        this.$confirm('是否确定停用网站?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            if (this.operatorData && this.operatorData.doQiYong) {
                for (let i = 0; i < this.selection.length; i++) {
                    let sendData: any = {};
                    sendData["id"] = this.selection[i].id
                    sendData["web_status"] = "tingyong"
                    sendData["type"] = "ty"
                    sendData["flag"] = false
                    if (i == this.selection.length - 1) { sendData["flag"] = true }
                    this.operatorData.doQiYong(sendData);
                }
            }
        }).catch(() => { })
    }

    rowEdit(ind: number, row: DemoData): void {
        if (this.listHandler && this.listHandler.rowEdit) {
            this.listHandler.rowEdit(ind, row);
        } else {
            console.warn("todo 初始化和实现行内容编辑");
        }
    }

    rowSet(ind: number, row: DemoData): void {
        if (this.listHandler && this.listHandler.rowSet) {
            this.listHandler.rowSet(ind, row);
        } else {
            console.warn("todo 初始化和实现行内容权限配置");
        }
    }

}